package com.ganee.agora.view

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ganee.agora.R
import com.ganee.agora.adapter.SignUpAdapter
import com.ganee.agora.model.SignUpEntity
import com.ganee.agora.model.TicketInfoEntity
import com.ganee.agora.viewmodel.HomeViewModel
import com.ganee.agora.widget.layoutmanager.GalleryLayoutManager
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_sign_up.*
import com.ganee.agora.widget.layoutmanager.ScaleTransformer
import com.ganee.baselib.httprequest.callback.RequestCallback
import kotlinx.android.synthetic.main.toolbar_with_title.*

@Route(path = Constance.ACTIVITY_URL_SIGN_UP)
class SignUpActivity : BaseActivity<HomeViewModel>(),View.OnClickListener {

    private var mAdapter: SignUpAdapter? = null
    private val mList:MutableList<TicketInfoEntity> = mutableListOf()

    private val emptyView: View
        get() {
            val view = LayoutInflater.from(this).inflate(R.layout.common_empty_data, signUpRecyclerView.parent as ViewGroup, false)
            return view
        }

    override fun getViewModel(): HomeViewModel {
        return ViewModelProviders.of(this).get(HomeViewModel::class.java)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_up)
        initView()
        initRequest("Past")
    }

    private fun initRequest(type:String) {
        if ("Past" == type) {
            viewModel.postPastTicketList {
                mList.clear()
                mList.addAll(it!!)
                mAdapter!!.notifyDataSetChanged()

                if (mList.size > 0){
                    sign_up_rl_comment.visibility = View.VISIBLE
                } else {
                    sign_up_rl_comment.visibility = View.GONE
                }
            }
        } else if ("UpComming" == type){
            viewModel.postUpCommingTicketList {
                mList.clear()
                mList.addAll(it!!)
                mAdapter!!.notifyDataSetChanged()
            }
        }

    }

    private fun initView() {

        toolbarTitle.text = "Sign-ups"
        toolbarCancel.setOnClickListener(this)
        sign_up_tv_past.setOnClickListener(this)
        sign_up_tv_upcomming.setOnClickListener(this)
        sign_up_tv_past.setTextColor(ContextCompat.getColor(this,R.color.color_login_yellow))
        sign_up_tv_upcomming.setTextColor(ContextCompat.getColor(this,R.color.white))

        val layoutManager = GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL)
        layoutManager.attach(signUpRecyclerView)
        layoutManager.setItemTransformer(ScaleTransformer())
        layoutManager.setCallbackInFling(true)
        layoutManager.setOnItemSelectedListener(object : GalleryLayoutManager.OnItemSelectedListener {
            override fun onItemSelected(recyclerView: RecyclerView?, item: View?, position: Int) {
                Log.e("eee",position.toString())
            }
        })
        mAdapter = SignUpAdapter(mList)
        mAdapter!!.emptyView = emptyView
        mAdapter!!.onItemClickListener = object : BaseQuickAdapter.OnItemClickListener {

            override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
                val entity  = adapter!!.getItem(position) as TicketInfoEntity
                ARouter.getInstance()
                        .build(Constance.ACTIVITY_URL_TICKET_DETAIL)
                        .withString("ticketId",entity.ticketId)
                        .navigation(this@SignUpActivity)
            }
        }
        signUpRecyclerView.adapter =  mAdapter
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.toolbarCancel -> {
                finish()
            }
            R.id.sign_up_tv_past -> {
                sign_up_tv_past.setTextColor(ContextCompat.getColor(this,R.color.color_login_yellow))
                sign_up_tv_upcomming.setTextColor(ContextCompat.getColor(this,R.color.white))
                initRequest("Past")
                sign_up_rl_show.visibility = View.GONE
            }
            R.id.sign_up_tv_upcomming -> {
                sign_up_tv_past.setTextColor(ContextCompat.getColor(this,R.color.white))
                sign_up_tv_upcomming.setTextColor(ContextCompat.getColor(this,R.color.color_login_yellow))
                initRequest("UpComming")
                sign_up_rl_comment.visibility = View.GONE
                sign_up_rl_show.visibility = View.VISIBLE
            }
        }
    }
}
